//
//  ViewController.m
//  loadImages
//
//  Created by Enrica on 16/7/20.
//  Copyright © 2016年 Enrica. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

// 图片属性
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

/** 加载站立图片 */
@property (strong, nonatomic) NSArray *standImages;

/** 加载小招图片 */
@property (strong, nonatomic) NSArray *smallImages;

/** 加载大招图片 */
@property (strong, nonatomic) NSArray *bigImages;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIImageView *imageView = [[UIImageView alloc] init];  // 创建imageView对象
//    imageView.frame = CGRectMake(100, 200, 265, 395);  // 设置imageView的尺寸
//    imageView.center = self.view.center;  // 让图片在中间位置显示
//    imageView.image = [UIImage imageNamed:@"1"];  // 加载图片
//    [self.view addSubview:imageView];  // 显示图片
    
    
    
//    UIImageView *imageView = [[UIImageView alloc] init];  // 创建imageView对象
//    imageView.frame = CGRectMake(0, 0, 265, 395);  // 设置imageView的尺寸
//    imageView.center = self.view.center;  // 让图片在中间位置显示
//    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];  // 获取图片资源所在路径
//    imageView.image = [UIImage imageWithContentsOfFile:imagePath];  // 加载图片
//    [self.view addSubview:imageView];  // 显示图片
    
    
    // 加载站立图片
    self.standImages = [self loadImagesWithImagePrefixName:@"stand" count:10];
    
    // 加载小招图片
    self.smallImages =  [self loadImagesWithImagePrefixName:@"xiaozhao2" count:35];
    
    // 加载大招图片
    self.bigImages =  [self loadImagesWithImagePrefixName:@"dazhao" count:87];
    
    // 程序运行时站立
    [self stand];
}

// 站立
- (IBAction)stand {
    
    [self movesWithImages:self.standImages repeatCount:0 duration:1.0 isStand:YES];
}

// 发小招
- (IBAction)smallMoves {
    
    [self movesWithImages:self.smallImages repeatCount:0 duration:2.0 isStand:NO];
}

// 发大招
- (IBAction)bigMoves {
    
    [self movesWithImages:self.bigImages repeatCount:0 duration:4.0 isStand:NO];
}

// 游戏结束
- (IBAction)gameOver {
    self.standImages = nil;
    self.smallImages = nil;
    self.bigImages = nil;
    
    self.imageView.animationImages = nil;
}

// 抽取加载图片重复的代码
- (NSArray *)loadImagesWithImagePrefixName:(NSString *)prefixName count:(int)count  {
    NSMutableArray<UIImage *> *images = [NSMutableArray array];  // 创建一个可变数组，用于装载图片
    for (int i = 0; i < count; i++) {
        
        NSString *imageName = [NSString stringWithFormat:@"%@_%d", prefixName, i + 1];
        
        //  UIImage *image = [UIImage imageNamed:imageName];
        // 获取图片的名称
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        // 获取图片在资源包中的路径
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];  // 加载图片
        [images addObject:image];  // 将图片添加到可变数组中
        
    }
    return images;  // 返回数组
}

// 抽取发招的方法
- (void)movesWithImages:(NSArray *)images repeatCount:(int)repeatCount duration:(float)duration isStand:(BOOL)isStand {
    self.imageView.animationImages = images;  // 设置动画
    self.imageView.animationRepeatCount = repeatCount;  // 设置动画播放的次数
    self.imageView.animationDuration = duration;  // 设置动画播放的时长
    
    [self.imageView startAnimating];  // 开始播放动画
    
    if (!isStand) {
        [self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];  // 延时加载
    }
}

@end
